<?php
class workdays
{
	function __construct()
	{
		echo ('
		<link rel="stylesheet" type="text/css" href="style/workdays.css" />
		<script type="text/javascript" src="scripts/workdays.js">
		</script>
		');
	}
	function showForm($id)
	{
		echo ('
		<div class="WDform" id="'.$id.'">
			<input name="schedule'.$id.'" type="radio" value="24" id="'.$id.'24" checked class="24">Круглосуточно<br />
			<input name="schedule'.$id.'" type="radio" value="alldays" id="'.$id.'alldays" class="alldays">Ежедневно<br />
			<div class="ADselect" id="ADselect'.$id.'" >
			</div>
			<input name="schedule'.$id.'" type="radio" value="notalldays" id="'.$id.'notalldays" class="notalldays">Выбрать дни<br />
			<div class="NADselect" id="NADselect'.$id.'" style="display: none;">
			</div>
		</div>
		');
	}
	function getWorkdays($id)
	{
		if($_POST['schedule'.$id.''])
		{
			if($_POST['schedule'.$id.'']=='24')
			$wd='24';
			elseif($_POST['schedule'.$id.'']=='alldays')
			{
				$from=$_POST['fromH'.$id.''].':'.$_POST['fromM'.$id.''];
				$to=$_POST['toH'.$id.''].':'.$_POST['toM'.$id.''];
				$wd='alldays~'.$from.'-'.$to;
			}
			elseif($_POST['schedule'.$id.'']=='notalldays')
			{
				$weekdays=array('mon', 'tue', 'wed', 'thi', 'fri', 'sat', 'sun');
				for($i=0; $i<7; $i++)
				{
					if($_POST['fromH'.$id.$weekdays[$i]]&&$_POST['fromM'.$id.$weekdays[$i]]&&$_POST['toH'.$id.$weekdays[$i]]&&$_POST['toM'.$id.$weekdays[$i]])
						$day[$weekdays[$i]]=$_POST['fromH'.$id.$weekdays[$i]].':'.$_POST['fromM'.$id.$weekdays[$i]].'-'.$_POST['toH'.$id.$weekdays[$i]].':'.$_POST['toM'.$id.$weekdays[$i]];
					if($_POST['fromHbreak'.$id.$weekdays[$i]]&&$_POST['fromMbreak'.$id.$weekdays[$i]]&&$_POST['toHbreak'.$id.$weekdays[$i]]&&$_POST['toMbreak'.$id.$weekdays[$i]])
						$break[$weekdays[$i]]=$_POST['fromHbreak'.$id.$weekdays[$i]].':'.$_POST['fromMbreak'.$id.$weekdays[$i]].'-'.$_POST['toHbreak'.$id.$weekdays[$i]].':'.$_POST['toMbreak'.$id.$weekdays[$i]];
				}
				foreach($day as $weekDay=>$vol)
				{
					$wd[]=$weekDay.'&'.$vol.'&'.$break[$weekDay];
				}
				$wd='notevery~'.implode('$', $wd);
			}
			return($wd);
		}
	}
	function showWorkdays($workdays)
	{
		$workdays=explode('~', $workdays);
		if($workdays[0]=='notevery')
		{
			$workdays=$workdays[1];
			$wdRus=array(
				'mon'=>'пн',
				'tue'=>'вт',
				'wed'=>'ср',
				'thi'=>'чт',
				'fri'=>'пт',
				'sat'=>'сб',
				'sun'=>'вс'
			);
			$workdays=explode('$', $workdays);
			for($i=0; $i<count($workdays); $i++)
			{
				$weekdaySc=explode('&', $workdays[$i]);
				$weekday=$weekdaySc[0];
				$worktime=$weekdaySc[1];
				$breaktime=$weekdaySc[2];
				$arr[$worktime.'^'.$breaktime].='-'.$wdRus[$weekday];
			}
			foreach($arr as $time=>$weekdays)
			{
				$weekArray[]=substr($weekdays, 1).'^'.$time;
			}
			$workdays=implode('|', $weekArray);
			for($i=0; $i<count($weekArray); $i++)
			{
				$weekdaySc=explode('^', $weekArray[$i]);

				$weekday=$weekdaySc[0];
				$wd=explode('-', $weekday);
				if($wd[0]=='пн' && $wd[1]=='вт' && $wd[2]=='ср')unset($wd[1]);
				if($wd[0]=='пн' && $wd[2]=='ср' && $wd[3]=='чт')unset($wd[2]);
				if($wd[0]=='пн' && $wd[3]=='чт' && $wd[4]=='пт')unset($wd[3]);
				if($wd[0]=='пн' && $wd[4]=='пт' && $wd[5]=='сб')unset($wd[4]);
				if($wd[0]=='пн' && $wd[5]=='сб' && $wd[6]=='вс')unset($wd[5]);
				$weekday=implode('-', $wd);
				$worktime=$weekdaySc[1];
				$breaktime=$weekdaySc[2];
				$timeAr=explode('-', $worktime);
				$openTime=$timeAr[0];
				$closeTime=$timeAr[1];
				$openTimeArray=explode(':', $openTime);
				$openHour=$openTimeArray[0];
				$openMin=$openTimeArray[1];
				if($openHour[0]=="0")$openHour=substr($openHour, 1);
				if($openMin=="00")$openMin="";
				$closeTimeArray=explode(':', $closeTime);
				$closeHour=$closeTimeArray[0];
				$closeMin=$closeTimeArray[1];
				if($closeHour[0]=="0")$closeHour=substr($closeHour, 1);
				if($closeMin=="00")$closeMin="";

				echo ('
				<div class="workdays">
					<div class="weekday">
						'.$weekday.'
					</div>
					<div class="worktime">
						<hour>'.$openHour.'</hour><min>'.$openMin.'</min>-<hour>'.$closeHour.'</hour><min>'.$closeMin.'</min>
					</div>
				');
				$timeAr=explode('-', $breaktime);
				$openTime=$timeAr[0];
				$closeTime=$timeAr[1];
				$openTimeArray=explode(':', $openTime);
				$openHour=$openTimeArray[0];
				$openMin=$openTimeArray[1];
				if($openHour[0]=="0")$openHour=substr($openHour, 1);
				if($openMin=="00")$openMin="";
				$closeTimeArray=explode(':', $closeTime);
				$closeHour=$closeTimeArray[0];
				$closeMin=$closeTimeArray[1];
				if($closeHour[0]=="0")$closeHour=substr($closeHour, 1);
				if($closeMin=="00")$closeMin="";
				if($openHour && $openMin && $closeHour && $closeMin)
				{
					echo ('
					<div class="breaktime">
						<hour>'.$openHour.'</hour><min>'.$openMin.'</min>-<hour>'.$closeHour.'</hour><min>'.$closeMin.'</min>
					</div>
					');
				}
				echo ('
				</div>
				');
	    	}
	  	}
	  	elseif($workdays[0]=='24')
	  		echo 'круглосуточно';
	 	elseif($workdays[0]=='alldays')
		{
			echo 'ежедневно '.$workdays[1];
		}
		else
			echo $workdays[0];
	}
	function editWorkdays($workdays, $id)
	{		$checked1='';
		$checked2='';
		$checked3='';		$workdays=explode('~', $workdays);
		if($workdays[0]=='notevery')
		{			$checked3='checked';
			$wdRus=array(
				'mon'=>'пн',
				'tue'=>'вт',
				'wed'=>'ср',
				'thi'=>'чт',
				'fri'=>'пт',
				'sat'=>'сб',
				'sun'=>'вс'
			);
		}
		if($workdays[0]=='alldays')
		{			$checked2='checked';			$time=explode('-', $workdays[1]);
			$from=$time[0];
			$to=$time[1];
			$fromTime=explode(':', $from);
			$toTime=explode(':', $to);
			$fromHour=$fromTime[0];
			$fromMinute=$fromTime[1];
			$toHour=$toTime[0];
			$toMinute=$toTime[1];
		}
		if($workdays[0]=='24' || !$workdays[0])$checked1='checked';
		echo ('
		<div class="WDform" id="'.$id.'">
			<input name="schedule'.$id.'" type="radio" value="24" id="'.$id.'24" '.$checked1.' class="24">Круглосуточно<br />
			<input name="schedule'.$id.'" type="radio" value="alldays" id="'.$id.'alldays" '.$checked2.' class="alldays">Ежедневно<br />
			<div class="ADselect" id="ADselect'.$id.'" '.$ADstyle.'>
		');
		if($workdays[0]=='alldays')
		{
			echo '
			c
			<select size="1" id="fromH'.$id.'" name="fromH'.$id.'">
			';
			for($i=0; $i<24; $i++)
			{
				$selected='';
				$hour=$i;
				if($i<10)$hour='0'.$i;
				if($hour==$fromHour)$selected='selected';
				echo ('
				<option value="'.$hour.'" '.$selected.'>'.$hour.'</option>
				');
			}
			echo '
			</select>:
			<select size="1" id="fromM'.$id.'" name="fromM'.$id.'">
			';
			for($i=0; $i<60; $i++)
			{
				$selected='';
				$minute=$i;
				if($i<10)$minute='0'.$i;
				if($minute==$fromMinute)$selected='selected';
				echo ('
				<option value="'.$minute.'" '.$selected.'>'.$minute.'</option>
				');
			}
			echo '
			</select>
			по
			<select size="1" id="toH'.$id.'" name="toH'.$id.'">
			';
			for($i=0; $i<24; $i++)
			{
				$selected='';
				$hour=$i;
				if($i<10)$hour='0'.$i;
				if($hour==$toHour)$selected='selected';
				echo ('
				<option value="'.$hour.'" '.$selected.'>'.$hour.'</option>
				');
			}
			echo '
			</select>:
			<select size="1" id="toM'.$id.'" name="toM'.$id.'">
			';
			for($i=0; $i<60; $i++)
			{
				$selected='';
				$minute=$i;
				if($i<10)$minute='0'.$i;
				if($minute==$toMinute)$selected='selected';
				echo ('
				<option value="'.$minute.'" '.$selected.'>'.$minute.'</option>
				');
			}
			echo '
			</select>
			';
		}
		if($workdays[0]=='notevery')
		{			$workday=explode('$', $workdays[1]);
			$wd=array();
			for($i=0; $i<count($workday); $i++)
			{
				$dayEls=explode('&', $workday[$i]);
				$weekday=$dayEls[0];
				$time=$dayEls[1];
				$wd[$weekday]=$time;			}
			foreach($wdRus as $eng=>$rus)
			{				$checked='';				if($wd[$eng])$checked='checked';				echo ('
	            <div id="'.$id.$eng.'">
					<input class="weekdays" id="'.$id.$eng.'Input" name="'.$id.'days[]" type="checkbox" value="'.$id.$eng.'" '.$checked.'>'.$rus.'
				');
				if($wd[$eng])
				{					echo ('
					<div class="'.$id.$eng.'chooseTime">
					');
					$time=explode('-', $wd[$eng]);
					$from=$time[0];
					$to=$time[1];
					$fromTime=explode(':', $from);
					$toTime=explode(':', $to);
					$fromHour=$fromTime[0];
					$fromMinute=$fromTime[1];
					$toHour=$toTime[0];
					$toMinute=$toTime[1];
					echo '
						c
						<select size="1" id="fromH'.$id.$eng.'" name="fromH'.$id.$eng.'">
						';
						for($i=0; $i<24; $i++)
						{
							$selected='';
							$hour=$i;
							if($i<10)$hour='0'.$i;
							if($hour==$fromHour)$selected='selected';
							echo ('
							<option value="'.$hour.'" '.$selected.'>'.$hour.'</option>
							');
						}
						echo '
						</select>:
						<select size="1" id="fromM'.$id.$eng.'" name="fromM'.$id.$eng.'">
						';
						for($i=0; $i<60; $i++)
						{
							$selected='';
							$minute=$i;
							if($i<10)$minute='0'.$i;
							if($minute==$fromMinute)$selected='selected';
							echo ('
							<option value="'.$minute.'" '.$selected.'>'.$minute.'</option>
							');
						}
						echo '
						</select>
						по
						<select size="1" id="toH'.$id.$eng.'" name="toH'.$id.$eng.'">
						';
						for($i=0; $i<24; $i++)
						{
							$selected='';
							$hour=$i;
							if($i<10)$hour='0'.$i;
							if($hour==$toHour)$selected='selected';
							echo ('
							<option value="'.$hour.'" '.$selected.'>'.$hour.'</option>
							');
						}
						echo '
						</select>:
						<select size="1" id="toM'.$id.$eng.'" name="toM'.$id.$eng.'">
						';
						for($i=0; $i<60; $i++)
						{
							$selected='';
							$minute=$i;
							if($i<10)$minute='0'.$i;
							if($minute==$toMinute)$selected='selected';
							echo ('
							<option value="'.$minute.'" '.$selected.'>'.$minute.'</option>
							');
						}
						echo '
						</select>
						';
					echo ('
					</div>
					');
				}
				echo ('
				</div>
				');
			}		}
		echo ('
			</div>
			<input name="schedule'.$id.'" type="radio" value="notalldays" id="'.$id.'notalldays" '.$checked3.' class="notalldays">Выбрать дни<br />
			<div class="NADselect" id="NADselect'.$id.'" style="display: none;">
			</div>
		</div>
		');
	}
}